package com.csu.mall.product.app;

import com.csu.mall.common.dto.AttrTo;
import com.csu.mall.common.utils.PageUtils;
import com.csu.mall.common.utils.R;
import com.csu.mall.product.Global;
import com.csu.mall.product.entity.ProductAttrValueEntity;
import com.csu.mall.product.service.AttrService;
import com.csu.mall.product.service.impl.ProductAttrValueService;
import com.csu.mall.product.vo.AttrResVo;
import com.csu.mall.product.vo.AttrVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author zhenhuanli
 * @email 743879362@qq.com
 * @date 2022-09-16 11:42:54
 */
@RestController
@RequestMapping("product/attr")
@Slf4j
public class AttrController {
    @Resource
    private AttrService attrService;

    @Resource
    private ProductAttrValueService productAttrValueService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrResVo attrResVo = attrService.getAttrResVo(attrId);
        return R.ok().put("attr", attrResVo);
    }


    @RequestMapping("/attrInfo/{attrId}")
    public AttrTo attrInfo(@PathVariable("attrId") Long attrId){
        return attrService.getAttrResTo(attrId);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
//		attrService.save(attr);
        attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);
        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    @GetMapping("/base/list/{catelogId}")
    public R selectBaseAttrs(@PathVariable("catelogId") Long catelogId, @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.queryPage(catelogId, params, Global.AttrType.BASE_ATTR.getCode());
        return R.ok().put("page", pageUtils);
    }

    @GetMapping("/sale/list/{catelogId}")
    public R selectSaleAttrs(@PathVariable("catelogId") Long catelogId, @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.queryPage(catelogId, params, Global.AttrType.SALE_ATTR.getCode());
        return R.ok().put("page", pageUtils);
    }
///product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public R listforSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> list = productAttrValueService.batchSelectProductAttrValue(spuId);
        return R.ok().put("data", list);
    }
    // /product/attr/update/{spuId}

    @PostMapping("/update/{spuId}")
    public R updateSpuAttrValueEntity(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> attrValueEntities){
        attrService.updateSpuAttrValueEntity(spuId, attrValueEntities);
        return R.ok();
    }
}
